package com.mgface.alg.链表;

/**
 * @author wanyuxiang
 * @version 1.0
 * @project Java_Alg
 * @create 2019-10-31 14:01
 **/
public class Test {
    static class Node {
        int value;
        Node next;

        @Override
        public String toString() {
            return "" + value;
        }
    }

    private static void reverse(Node head) {
        if (head == null) {
            return;
        }
        Node previous = null;
        Node current = null;
        Node next = null;
        current = head.next;
        next = current.next;
        current.next = null;
        previous = current;
        current = next;
        while (current.next != null) {
            next = current.next;
            current.next = previous;
            previous = current;
            current = next;
        }
        current.next = previous;
        head.next = current;
    }

    private static void reverse2(Node head) {
        if (head == null) {
            return;
        }
        Node current = head.next.next;
        //排序之后节点
        head.next.next = null;
        while (current!=null){
           Node next = current.next;
           current.next = head.next;
           head.next = current;
           current = next;
        }
    }

    public static void main(String[] args) {
        Node head = new Node();
        Node current = head;
        for (int i = 0; i < 8; i++) {
            Node temp = new Node();
            temp.value = i;
            current.next = temp;
            current = temp;
        }

        for (current = head.next; current != null; current = current.next) {
            System.out.print(current.value + " ");
        }
        System.out.print("\n逆行之后:\n");
        reverse2(head);
        for (current = head.next; current != null; current = current.next) {
            System.out.print(current.value + " ");
        }
        System.out.println("\n");

    }
}
